function tri = template_mesh_tri(d)
% function tri = template_mesh_tri(d)
%
% Generate a template mesh for single triangle.
% The nodes coordinates are not given but assumed  to be
% sorted as well as in the logical order given by indices(d),
% however, the logical mesh connection is returned for degree d.
% For example, when d=3, it looks like
%               1
%             /   \
%           2  -   3
%         /   \    /  \
%      4  ---  5 --- 6
%     /   \   /    \  /   \
%   7 --- 8 --- 9 --- 10

tri = zeros(d^2,3);% have d^2 elements
tri_begin = 1;
node_begin = 1;
for i = 1:d
    % first the \triangle
    tri(tri_begin:2:tri_begin+2*i-2,1) = node_begin:node_begin+i-1; %  line(i,1:2:end)
    tri(tri_begin:2:tri_begin+2*i-2,2)= node_begin+i:node_begin+i+i-1; %  line(i+1,1:end-1)
    tri(tri_begin:2:tri_begin+2*i-2,3)= node_begin+i+1:node_begin+i+i; %  line(i+1,2:end)
    % then the \gradient
    tri(tri_begin+1:2:tri_begin+2*i-2,1) = node_begin:node_begin+i-2; %  line(i,1:end-1)
    tri(tri_begin+1:2:tri_begin+2*i-2,2)= node_begin+1:node_begin+i-1; %  line(i,2:end)
    tri(tri_begin+1:2:tri_begin+2*i-2,3)= node_begin+i+1:node_begin+i+i-1; %   line(i+1,2:end-1)
    %prepare for next line;
    tri_begin = tri_begin + 2*i-1;
    node_begin = node_begin + i;
end